package com.blt.car.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blt.car.R;
import com.blt.car.journey.JourneyDetailActivity;

import blt.app_core.api.base.CoreEvent;
import blt.app_core.api.base.CoreManager;
import blt.app_core.api.journey.JourneyApi;
import blt.app_core.api.other.OutApi;

/**
 * Created by xgp on 2017/6/23.
 */

public class RedPackageDialog extends Dialog {
    private int travelOrderId;

    public RedPackageDialog(Context context, int travelOrderId) {
        super(context, R.style.CommonDialog);
        setCancelable(false);
        setCanceledOnTouchOutside(true);
        CoreManager.addClient(this);
        this.travelOrderId = travelOrderId;
    }


    private RelativeLayout container;
    private RelativeLayout openBefore;
    private RelativeLayout openAfter;
    private ImageView openRed;

    private ImageView getRed;

    private TextView tvMonney;



    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        window.setGravity(Gravity.CENTER);  //此处可以设置dialog显示的位置
        window.setWindowAnimations(R.style.Dialog_Anim);  //添加动画
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_red_package);
        container = (RelativeLayout) findViewById(R.id.dialog_container);
        openBefore = (RelativeLayout) findViewById(R.id.open_before);
        openAfter = (RelativeLayout) findViewById(R.id.open_after);
        openRed = (ImageView) findViewById(R.id.dialog_open_red);
        getRed = (ImageView) findViewById(R.id.dialog_get_red);
        tvMonney = (TextView) findViewById(R.id.dialog_red_account);
        openRed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openBefore.setVisibility(View.GONE);
                openAfter.setVisibility(View.VISIBLE);
                CoreManager.getCore(JourneyApi.class).snatchRedpacket(travelOrderId);
                CoreManager.notifyClients(OutApi.class,OutApi.onLoot,true);
            }
        });

        getRed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "领取成功", Toast.LENGTH_SHORT).show();
                dismiss();
            }
        });

        container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    @CoreEvent(coreClientClass = JourneyApi.class)
    public void onRedPackage(String monney) {
        tvMonney.setText(monney);
    }



}
